package com.lst.projectlib.component.guidegallery;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.lst.projectlib.R;
import com.lst.projectlib.entity.News;
import com.lst.projectlib.util.DensityUtil;
import com.lst.projectlib.util.ImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/11.
 */

public class GuideGalleryWithPoint extends RelativeLayout {
    GuideGallery guideGallery;
    GuideGalleryIndicator indicator;
    List<News> list = new ArrayList<News>();

    public GuideGalleryWithPoint(Context context) {
        super(context);
        init();
    }

    public GuideGalleryWithPoint(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GuideGalleryWithPoint(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        guideGallery = new GuideGallery(getContext());
        guideGallery.setSoundEffectsEnabled(false);
        LayoutParams galleyLP = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(guideGallery, galleyLP);

        indicator = new GuideGalleryIndicator(getContext());
        indicator.setDefaultPic(R.drawable.dot_normal);
        indicator.setActivePic(R.drawable.dot_focused);
        indicator.setBackgroundColor(getResources().getColor(R.color.alpha5));
        int leftRightPadding = DensityUtil.dip2px(getContext(),8);
        int topBottomPadding = DensityUtil.dip2px(getContext(),3);
        indicator.setPadding(leftRightPadding, topBottomPadding, leftRightPadding, topBottomPadding);
        LayoutParams indicatorLP = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(getContext(),42));
        indicatorLP.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addView(indicator, indicatorLP);

        guideGallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                indicator.changePointView((int) id);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener){
        guideGallery.setOnItemClickListener(onItemClickListener);
    }

    public void notifyAdapter(List<News> list){
        this.list = list;

        GuideGalleryAdapter mAdapter  = new GuideGalleryAdapter();
        guideGallery.setAdapter(mAdapter);

        guideGallery.setTotalSize(list.size());

        indicator.setData(list);
    }

    public void startAutoScroll(){
        guideGallery.startAutoScroll();
    }

    public void stopAutoScroll(){
        guideGallery.stopAutoScroll();
    }

    class GuideGalleryAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (list.size() > 0) {
                return Integer.MAX_VALUE;
            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int position) {
            return list.get(position % list.size());
        }

        @Override
        public long getItemId(int position) {
            return position % list.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(getContext());
            imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.MATCH_PARENT, Gallery.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            News news = (News) getItem(position);

            String url = news.getSmallPictureUrl();

            if (TextUtils.isEmpty(url)) {
                imageView.setImageResource(R.drawable.img_loaderror);
            } else {
                ImageLoaderUtil.getInstance().displayImage(url, imageView, false, 0);
            }

            return imageView;
        }
    }
}
